package net.jalisq;

import java.io.File;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSetMetaData;
import java.util.*;

import net.jalisq.gen.JoinCodeGen;

import wulfUtils.db.*;
import wulfUtils.io.IOUtil;
import wulfUtils.string.StringUtil;
import wulfUtils.template.StringTemplate;

import static java.lang.String.*;

public class Runner {


	private static String JDBC_PROTO = "jdbc:mysql";
	private static String JDBC_HOST	 = "localhost";
	/*Changes for MYSQL5.1*/
    /*private static String JDBC_HOST	 = "localhost";*/
	
	private static String JDBC_SCHEMA = "systemdb_prod";
	
	private static String JDBC_USER = "root";
	private static String JDBC_PASSWORD = "ww";
	
	private static final String[] arr_reserved = { 
		"abstract","continue","for","new","switch","assert","default","goto","package","synchronized","boolean","do","if","private","this","break","double","implements","protected","throw","byte","else","import","public","throws","case","enum","instanceof","return","transient","catch","extends","int","short","try","char","final","interface","static","void","class","finally","long","strictfp","volatile","const","float","native","super","while"
	};
	private static final Set<String> reserved = new HashSet<String>(Arrays.asList(arr_reserved)); 
	
		
	private static String JDBC_STRING = JDBC_PROTO + "://" + JDBC_HOST + "/" + JDBC_SCHEMA + "?user="  + JDBC_USER + "&password=" + JDBC_PASSWORD;
	
		public static void main(String[] args) {
			
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		}catch(Throwable t){
			throw new RuntimeException("Could not load mysql jdbc driver!",t);
			
		}
		String pack = "test.gen";
		String FILEEXT = "jav";
		String DELIM = "%";
		
		String java_src_dir =  "src\\main\\java\\";
		String package_dir = java_src_dir + pack.replace(".","\\");
		
		// generate joins //
	
		
		try{
			new JoinCodeGen().generate(java_src_dir, "wulfUtils.linq.builder.joins", "Joinable");
			StringTemplate schema = StringTemplate.loadTemplate("wulfUtils.linq.templates.schema", FILEEXT, DELIM);
			schema.put("schema", JDBC_SCHEMA);
			schema.put("package", pack);
			
			Connection con = DriverManager.getConnection(JDBC_STRING);
			DatabaseMetaData dmd = con.getMetaData();
			
			
			List<Map<String, Object>> tables = DBUtils.resultSetAsMaps(dmd.getTables(null, null, null, null));
			schema.put("tables", tables);
			
			IOUtil.write( package_dir + "\\schema",JDBC_SCHEMA + ".java", schema.fillTemplate());
			
			
			for( Map<String, Object> t : tables) {
				StringTemplate table = StringTemplate.loadTemplate("wulfUtils.linq.templates.table", FILEEXT, DELIM);
				table.setData(t);
				table.put("package", pack);
				table.put("schema", schema.getData().get("schema"));
				String table_name = (String) t.get("TABLE_NAME");
				table.put("columns", DBUtils.resultSetAsMaps( dmd.getColumns(null, null, table_name, null)));
				for(Map<String, Object> cols : (List<Map<String, Object>>) table.getData().get("columns") ) {
					String colname = (String) cols.get("COLUMN_NAME");
					cols.put("variableName" , colname);
					if(reserved.contains(colname.toLowerCase())) {
						cols.put("variableName", colname + "_");
					}
					
				}
				
				IOUtil.write( package_dir + "\\tables", table_name + ".java", table.fillTemplate());
				
				//System.out.println(table.getData().get("columns"));
				/**
				 * 
				List<Map<String, Object>> cols = new ArrayList<Map<String, Object>>();
				table.put("columns", cols);
				ResultSetMetaData rsmd = DBUtils.executeSQL(con, format( "select * from {0}", table_name)).getMetaData();
				
				for( int i = 1; i < rsmd.getColumnCount(); i++) 
				{
					Map<String, Object> coldata = new HashMap<String, Object>();
					cols.add(coldata);
					
					String colname = rsmd.getColumnName(i);
					
					String typename = rsmd.getColumnTypeName(i);
					coldata.put("name", colname.toLowerCase());
					coldata.put("raised_name", String.);
					coldata.put("type", typename);
					
				}
				
				**/
				/**
				List<Map<String, Object>> cols = DBUtils.resultSetAsMaps(dmd.getColumns(null, null , "dsssystem", null));
				for( Map<String, Object> c : cols) {
					java.sql.Types.
					System.out.println(c);
				}**/
			
			}
			
			
		} catch(Throwable t) {
			t.printStackTrace();
		}
		
	}
}
